var Comments = {
	kkk:'kkk',
	findComments:function(req, type, uid, callback){
		var user = new Array();
		req.comments.find({'type':type,'uid':uid}).toArray(function(err, items){
			if (err) {
				callback({status:400,data:err})
			} else {
				var commentsObj = items
				if (items.length===0 || items===null) {
					// res.status(404).send('no existence comments')
					callback({status:404,data:'no existence comments'})
				} else {
					for (var i = 0; i < items[0].comments.length; i++) {
						user.push(req.dbid(items[0].comments[i].user))
					}
					req.users.find({
						'_id':{
							'$in':user
						}},{
							_id:1,
							name:1,
							headImgUrl:1,
							LV:1
						}).toArray(function(err, items){
							if (err) {
								callback({status:400,data:err})
							} else {
								for (var i = 0; i < commentsObj[0].comments.length; i++) {
									for (var j = 0; j < items.length; j++) {
										if (commentsObj[0].comments[i].user == items[j]._id) {
											commentsObj[0].comments[i].user = items[j];
										}
									}
								}
								callback({status:200, data:commentsObj})
								// res.json({
								// 	status:200,
								// 	data:commentsObj
								// })
							}
						})
					}
				}
		})
	},
	insertComments:function(req, type, uid, callback){
		console.log('dddddd')
		req.comments.find({'type':type, 'uid':req.params.id}).toArray(function(err, items){
			if (err) {
				// res.status(400).send('Bad Request');
				callback({status:400,data:err})
			} else {
				if (items==null || items.length==0) {
					req.comments.insert({
						'type':type,
						'uid':req.params.id,
						'comments':[{
						'user':req.body.user.userid,
							'content':req.body.content
						}]
					},function(err, result){
						callback({status:200,data:'ok'})
					})
				} else {
					req.comments.update({
						'uid':req.params.id
					},{
						$push:{
							'comments':{
								'user':req.body.user.userid,
								'content':req.body.content
							}
						}
					}, function(err, result){
						callback({status:200,data:'ok'})
					})
				}
			}
		})
	}
}

module.exports = Comments;